const app = getApp()
import areaList from '../../config/area.js';
import {
  workCate,
  apply,
  getApply
} from "../../config/https";
//工种类型、入驻申请、获取重新提交申请页面数据
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList: areaList,
    nativeShow: false,              //省市区
    columns: '',
    workType: [],                 //工种类型
    wrokTypeArr: [],              //选中的工种类型
    maskShow: false,              //工种弹窗
    name: '',                     //姓名
    phone: '',                    //手机号码
    native_place: '',             //籍贯
    newplace: '',                 //现住址
    work_age: '',                 //工龄
    work_type: '',                //工种
    idcard: '',                   //身份证号码
    longitude: '',                //经度
    latitude: '',                 //纬度
    card_front_url: '',           //上传表单的身份证正面    
    card_after_url: '',           //上传表单的身份证反面 
    user_url: '',                 //上传表单的用户头像
    card_front_image: '',         //身份证正面
    card_after_image: '',         //身份证反面
    user_image: ''                //用户头像
  },
  //删除所选中的工种
  deleteWork(e){
    let index = e.currentTarget.dataset.index
    let wrokTypeArr = this.data.wrokTypeArr
    wrokTypeArr.splice(index,index + 1)
    this.setData({
      wrokTypeArr
    })
  },
  //工种弹窗 || 默认弹窗
  showMask() {
    this.setData({
      maskShow: true
    })
  },
  //关闭工种弹窗 || 关闭默认弹窗
  onClose() {
    this.setData({
      maskShow: false
    })
  },
  //获取选中弹窗值
  confirmClick(e) {
    let id = this.data.workType[e.detail.index].id
    let wrokTypeArr = this.data.wrokTypeArr
    wrokTypeArr.push({
      'name': e.detail.value,
      'id': id
    })
    if(wrokTypeArr.length > 2){
      wrokTypeArr.splice(0,1)
    }
    this.setData({
      wrokTypeArr
    })
    this.onClose()
  },
  //获取工种类型
  getWorkType(){
    workCate().then(res => {
      if(res.code == 1){
        let workType = res.data
        let newArr = workType.map(item => {
          return item.name
        })
        this.setData({
          columns: newArr,
          workType
        })
      }
    })
  },
  nativeClose(){
    this.setData({
      nativeShow: !this.data.nativeShow
    })
  },
  confirmNative(e){
    let selectVal = e.detail.values[0].name + e.detail.values[1].name + e.detail.values[2].name
    this.setData({
      native_place: selectVal,
      nativeShow: false
    })
  },
  // 现住址选择
  choosePlace() {
    let that = this
    wx.chooseLocation({
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          newplace: res.address
        })
      },
      fail: function () {
        wx.getSetting({
          success: function (res) {
            var statu = res.authSetting;
            if (!statu['scope.userLocation']) {
              wx.showModal({
                title: '是否授权当前位置',
                content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
                success: function (tip) {
                  if (tip.confirm) {
                    wx.openSetting({
                      success: function (data) {
                        if (data.authSetting["scope.userLocation"] === true) {
                          wx.showToast({
                            title: '授权成功',
                            icon: 'success',
                            duration: 1000
                          })
                          //授权成功之后，再调用chooseLocation选择地方
                          wx.chooseLocation({
                            success: function (res) {
                              that.setData({
                                latitude: res.latitude,
                                longitude: res.longitude,
                                newplace: res.address
                              })
                            },
                          })
                        } else {
                          app.$tip('授权失败')
                        }
                      }
                    })
                  }
                }
              })
            }
          },
          fail: function (res) {
            app.$tip('调用授权窗口失败')
          }
        })
      }
    })
  },
  // 获取重新提交申请页面数据
  getDate(type){
    getApply({
      token: wx.getStorageSync('token')
    }).then(res => {
      if(res.code == 1){
        this.setData({
          name: res.data.name,                            //姓名
          phone: res.data.mobile,                         //手机号码
          native_place: res.data.native_place,            //籍贯
          newplace: res.data.now_place,                   //现住址
          work_age: res.data.work_age,                    //工龄
          wrokTypeArr: res.data.work_cate_id,             //工种
          idcard: res.data.idcard,                        //身份证号码
          longitude: res.data.longitude,                  //经度
          latitude: res.data.latitude,                    //纬度
          card_front_image: res.data.card_front_image,    //身份证正面
          card_after_image: res.data.card_reverse_image,  //身份证反面
          user_image: res.data.body_image,                //用户头像
          card_front_url: res.data.card_front_image,      //上传表单的身份证正面    
          card_after_url: res.data.card_reverse_image,    //上传表单的身份证反面 
          user_url: res.data.body_image                   //上传表单的用户头像
        })
      }
    })
  },
  upImg(e) {
    let that = this
    let upType = e.currentTarget.dataset.type 
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      success: function (res) {
        if (!res.cancel) {
          console.log(res.tapIndex)
          if (res.tapIndex == 0) {
            that.chooseWxImage('album',upType)
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera',upType)
          }
        }
      }
    })
  },
  chooseWxImage(type,upType) {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      count: 1,
      success: function (res) {
        let image = res.tempFilePaths[0]
        wx.uploadFile({
          url: 'https://hsf.0791jr.com/api/Upload/upload',
          filePath: res.tempFilePaths[0],
          name: 'file',
          success(res) {
            let data = JSON.parse(res.data)
            let url = data.data
            if (data.code == 1) {
              if(upType == 1){
                that.setData({
                  card_front_image: url,
                  card_front_url: image
                })
              }else if(upType == 2){
                that.setData({
                  card_after_image: url,
                  card_after_url: image
                })
              }else if(upType == 3){
                that.setData({
                  user_image: url,
                  user_url: image
                })
              }
              
            }
          }
        })
      }
    })
  },
  submitForm(e) {
    let { value: info } = e.detail
    let wrokTypeArr = this.data.wrokTypeArr
    let wrokTypeId = []
    wrokTypeArr.map(item => {
      wrokTypeId.push(item.id)
    })
    console.log(wrokTypeId)
    const regPhone = /^1[23456789]\d{9}$/
    const regIdcard = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
    if(!info.name){
      app.$tip('请输入您的姓名')
      return
    }
    if (!info.phone) {
      app.$tip('请输入手机号码')
      return
    }
    if (!regPhone.test(info.phone)) {
      app.$tip('手机号格式不正确')
      return
    }
    if (!info.native_place) {
      app.$tip('请输入籍贯')
      return
    }
    if (!info.now_place) {
      app.$tip('请选择现住地址')
      return
    }
    if (!info.work_age) {
      app.$tip('请输入工龄')
      return
    }
    if (wrokTypeId.length == 0) {
      app.$tip('请输入工种')
      return
    }
    if (!regIdcard.test(info.idcard)) {
      app.$tip('请输入正确的身份证号')
      return
    }
    if (!this.data.card_front_image) {
      app.$tip('请上传身份证正面')
      return
    }
    if (!this.data.card_after_image) {
      app.$tip('请上传身份证反面')
      return
    }
    if (!this.data.user_image) {
      app.$tip('请上传上传上半身头像')
      return
    }
    apply({
      token: wx.getStorageSync('token'),
      name: info.name,
      mobile: info.phone,
      native_place: info.native_place,
      now_place: info.now_place,
      work_age: info.work_age,
      work_cate_id: wrokTypeId.join(','),
      idcard: info.idcard,
      card_front_image: this.data.card_front_image,
      card_reverse_image: this.data.card_after_image,
      body_image: this.data.user_image,
      latitude: this.data.latitude,
      longitude: this.data.longitude      
    }).then(res => {
      app.$tip(res.msg)
      if(res.code == 1){
        setTimeout(() => wx.navigateBack(),1500)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getWorkType()
    if(options.type){
      this.getDate()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})